package com.cskaoyan.wx.controller;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.wx.bo.CouponCodeBO;
import com.cskaoyan.wx.bo.CouponIdBO;
import com.cskaoyan.wx.bo.WxCouponBO;
import com.cskaoyan.wx.bo.WxFeedBackBO;
import com.cskaoyan.wx.service.WxCouponService;
import com.cskaoyan.wx.vo.WxCouponVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("wx")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    @GetMapping("coupon/list")
    public BaseRespVo couponList(WxCouponBO wxCouponBO){
        WxCouponVO wxCouponVO = wxCouponService.getCouponList(wxCouponBO);
        return BaseRespVo.ok(wxCouponVO);
    }

    @GetMapping("coupon/mylist")
    public BaseRespVo couponMyList(WxCouponBO wxCouponBO){
        WxCouponVO wxCouponVO = wxCouponService.getCouponMyList(wxCouponBO);
        return BaseRespVo.ok(wxCouponVO);
    }

    @PostMapping("coupon/receive")
    public BaseRespVo couponReceive(@RequestBody CouponIdBO couponIdBO){
        int code = wxCouponService.receiveCoupon(couponIdBO);
        if (code == 0) {
            return BaseRespVo.ok();
        }else if (code == 1){
            return BaseRespVo.fail("已拥有",702);
        }else if (code == 2){
            return BaseRespVo.fail("已拥有限领一张",702);
        }else {
            return BaseRespVo.fail("优惠券已领完",506);
        }
    }

    @PostMapping("coupon/exchange")
    public BaseRespVo exchangeCoupon(@RequestBody CouponCodeBO code){
        int temp = wxCouponService.exchangeCoupon(code);
        if (temp == 0){
            return BaseRespVo.ok();
        }else if (temp == 1){
            return BaseRespVo.fail("已拥有",702);
        }else if (temp == 2){
            return BaseRespVo.fail("已拥有限领一张",702);
        }else {
            return BaseRespVo.fail("优惠券已领完",702);
        }
    }

    @GetMapping("coupon/selectlist")
    public BaseRespVo selectListCoupon(Integer cartId,Integer grouponRulesId){
        List<Coupon> coupons = wxCouponService.selectMyCoupon();
        return BaseRespVo.ok(coupons);
    }

    @PostMapping("feedback/submit")
    public BaseRespVo feedbackSubmit(@RequestBody WxFeedBackBO wxFeedBackBO){
        wxCouponService.submitFeedBack(wxFeedBackBO);
        return BaseRespVo.ok();
    }
}
